home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / sound / sndplaydoublebuffer / _source / ldandfix.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  2.9 KB  |  90 lines

  1. /*
  2.     File:        LDandFix.c
  3.  
  4.     Contains:    Routines demonstrating how to convert to and from long double and Fixed types.
  5.  
  6.     Written by: Mark Cookson    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/31/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #include "LDandFix.h"
  25.  
  26. Fixed            ASoundLongDoubleToFix    (long double theLD)
  27. {
  28. /*
  29.     A Fixed number is of the type 12345.67890.  It is 32 bits in size with the
  30.     high order bits representing the significant value (that before the point)
  31.     and the lower 16 bits representing the fractional part of the number.
  32.  
  33.     The Sound Manager further complicates matters by using Fixed numbers, but
  34.     needing to represent numbers larger than what the Fixed is capable of.
  35.  
  36.     To do this the Sound Manager treats the sign bit as having the value 32768
  37.     which will cause any number greater or equal to 32768 to look like it is
  38.     negative.
  39.  
  40.     This routine is designed to "do the right thing" and convert any long double
  41.     into the Fixed number it represents.
  42.  
  43.     long double is the input type because AIFF files use extended80 numbers and
  44.     there are routines that will convert from an extended80 to a long double.
  45.  
  46.     A long double has far greater precision than a Fixed, so any number whose
  47.     significant or fraction is larger than 65535 will not convert correctly.
  48. */
  49.  
  50.     unsigned long    theResult        = 0;
  51.     unsigned short    theSignificant    = 0,
  52.                     theFraction        = 0;
  53.  
  54.     if (theLD < kMaxValue) {
  55.         theSignificant = theLD;
  56.         theFraction = theLD - theSignificant;
  57.         if (theFraction > kMaxValue) {
  58.             /* Won't be able to convert */
  59.             theSignificant    = 0;
  60.             theFraction        = 0;
  61.         }
  62.     }
  63.  
  64.     theResult |= theSignificant;
  65.     theResult = theResult << (sizeof (unsigned short) * kBitsPerByte);
  66.     theResult |= theFraction;
  67.  
  68.     return theResult;
  69. }
  70.  
  71. /*
  72.     I'm still trying to figure out why I need the correction factor, but the
  73.     number returned by this function is only for our own internal calculation
  74.     of buffer sizing so we can afford to loose a little precision in the
  75.     fraction.
  76. */
  77. long double        ASoundFixToLongDouble    (Fixed theFixed)
  78. {
  79.     long double        theResult    = 0.0;
  80.     unsigned short    high16Bits    = 0,
  81.                     low16Bits    = 0;
  82.  
  83.     high16Bits = theFixed >> (sizeof (unsigned short) * kBitsPerByte);
  84.     low16Bits = theFixed + 0x496E;        /* Correction factor */
  85.  
  86.     theResult = high16Bits + (low16Bits * kFraction);
  87.  
  88.     return theResult;
  89. }
  90.